/**
 * 渲染概览流程图描述的综述内容
 */
window.rendOverViewDesc = function (oOverViewContent, overViewData) {
    if (overViewData) {
        $('<p/>', {text: TempoUtils.locale("mine.mining_begin_time") + overViewData.startTime}).appendTo(oOverViewContent);
        $('<p/>', {text: TempoUtils.locale("mine.mining_finish_time") + overViewData.endTime}).appendTo(oOverViewContent);
        $('<p/>', {text: TempoUtils.locale("mine.mining_cost_time") + overViewData.durationTime}).appendTo(oOverViewContent);
        if (!overViewData.PNAME && !overViewData.PNAME) {
        } else {
            if (overViewData.PNAME == null || overViewData.PNAME == "null" || window.apiDesigner) {
                $('<p/>', {text: TempoUtils.locale("mine.analyse_api_theme_Name").replace("analysename", overViewData.THEMENAME)}).appendTo(oOverViewContent);
            } else {
                $('<p/>', {text: TempoUtils.locale("mine.analyse_Subject_Name").replace("subjectname", overViewData.PNAME).replace("analysename", overViewData.THEMENAME)}).appendTo(oOverViewContent);
            }
        }
        $('<p/>', {text: TempoUtils.locale("mine.node_Num_Names").replace("node_Num", overViewData.nodeNum).replace("node_Names", overViewData.nodeNames)}).appendTo(oOverViewContent);
    } else {
        $('<p/>').text(TempoUtils.locale("mine.liuchengweizhixing")).appendTo(oOverViewContent);
    }
}

/**
 * 渲染概览流程图描述的输入内容
 */
window.rendDescInput = function (oInputContentDiv, inputDatas) {
    if (inputDatas && inputDatas.length > 0) {
        for (var m = 0; m < inputDatas.length; m++) {
            var inputData = inputDatas[m];
            var inputContent = TempoUtils.locale("mine.inputContent");
            inputContent = inputContent.replace("col_Num", inputData.colNum).replace("row_Num", inputData.rowNum)
                .replace("str_Num", inputData.strNum).replace("date_Num", inputData.dateNum).replace("numric_Num", inputData.numricNum);
            if ($.trim(inputData.fullColNames).length == 0) {
                inputContent = inputContent.replace("full_Col_Names", TempoUtils.locale("mine.black"));
            } else {
                inputContent = inputContent.replace("full_Col_Names", inputData.fullColNames);
            }
            if ($.trim(inputData.missColNames).length == 0) {
                inputContent = inputContent.replace("miss_Col_Names", TempoUtils.locale("mine.black"));
            } else {
                inputContent = inputContent.replace("miss_Col_Names", inputData.missColNames);
            }
            $('<p/>', {text: inputContent}).appendTo(oInputContentDiv);
        }
    }
}

/**
 * 渲染概览流程图描述的过程内容
 */
window.rendPreprocessTable = function (oProcessContentDiv, processData) {
    if (!processData) {
        return;
    }
    $('<p/>', {
        text: TempoUtils.locale("mine.mine_preprocess_Num").replace("preprocess_Num", processData.length)
    }).appendTo(oProcessContentDiv);

    var oProcessContentTableDiv = $('<div/>', {
        class: "tempo-mine-input-flow-view-process-content-table"
    }).appendTo(oProcessContentDiv);
    var table = $('<table style="width:99%;"/>').appendTo(oProcessContentTableDiv);
    $('<thead><tr><th>' + TempoUtils.locale("mine.nodeName") + '</th><th>' + TempoUtils.locale("mine.nodeDescrib") + '</th></tr></thead>').appendTo(table);
    var tbody = '<tbody>';
    for (var i = 0; i < processData.length; i++) {
        var processDataItem = processData[i];
        var nodeDesc = TempoMineInsightUtils.getPreprocessNodeDesc(processDataItem);
        tbody += '<tr><td>' + processDataItem.nodeLabel + '</td><td>' + nodeDesc + '</td></tr>';
    }
    tbody += '</tbody>';
    $(tbody).appendTo(table);
}

/**
 * 渲染聚类算法的结果描述信息
 */
window.rendClusterResult = function (oResultContentDiv, clusterData) {
    if (!clusterData) {
        return;
    }
    for (var i = 0; i < clusterData.length; i++) {
        var clusterResult = clusterData[i];
        $('<p/>', {text: clusterResult.nodeName + TempoUtils.locale("mine.culsterResult")}).appendTo(oResultContentDiv);
        var data = clusterResult.data;
        for (var j = 0; j < data.length; j++) {
            var clusterClassDesc = TempoUtils.locale('mine.clusterclassdesc').replace('num', (j + 1) + "").replace('count', data[j]);
            $('<p/>', {text: clusterClassDesc}).appendTo(oResultContentDiv);
        }
    }
}

/**
 * 渲染分类算法的结果描述信息
 */
window.rendClassifiResult = function (oResultContentDiv, classifiData) {

}

/**
 * 渲染分类评估的结果描述信息
 */
window.rendClassifiEvalResult = function (oResultContentDiv, classifiEvalData) {
    if (!classifiEvalData) {
        return;
    }
    for (var i = 0; i < classifiEvalData.length; i++) {
        var classEvalDataItem = classifiEvalData[i].algName2CorrectRation;
        var arr = sortData(classEvalDataItem);
        for (var m = 0; m < arr.length; m++) {
            var data = arr[m];
            var algName = data.name;
            var correctRation = data.value;
            if (correctRation >= 0.85) {
                $('<p/>', {text: algName + TempoUtils.locale("mine.classResultPreferably")}).appendTo(oResultContentDiv);
            } else if (correctRation >= 0.7) {
                $('<p/>', {text: algName + TempoUtils.locale("mine.classResultWell")}).appendTo(oResultContentDiv);
            } else if (correctRation >= 0) {
                $('<p/>', {text: algName + TempoUtils.locale("mine.classResultBad")}).appendTo(oResultContentDiv);
            }
            $('<p/>', {text: TempoUtils.locale("mine.classRelativeError") + TempoMineUtils.toPercent(correctRation)}).appendTo(oResultContentDiv);
        }
    }
}
/**
 * 分类、聚类评估结果需要按照结果做升序排列
 */
window.ortData = function (data) {
    var dataArr = [];
    var dataName
    for (var name in data) {
        dataArr.push({name: name, value: data[name]});
    }
    dataArr.sort(function (a, b) {
        return a.value - b.value
    })
    return dataArr;
}

/**
 * 渲染回归算法的结果描述信息
 */
window.rendRegressionResult = function (oResultContentDiv, regressionData) {

}

/**
 * 渲染回归评估的结果描述信息
 */
window.rendRegressionEvalResult = function (oResultContentDiv, regressionEvalData) {
    if (!regressionEvalData) {
        return;
    }
    for (var i = 0; i < regressionEvalData.length; i++) {
        var regressionEvalItem = regressionEvalData[i].algName2relativeError;
        var arr = sortData(regressionEvalItem);
        for (var m = 0; m < arr.length; m++) {
            var data = arr[m];
            var algName = data.name;
            var relativeError = data.value;
            if (relativeError <= 0.05) {
                $('<p/>', {text: algName + TempoUtils.locale("mine.regressionResultPreferably")}).appendTo(oResultContentDiv);
            } else if (relativeError <= 0.1) {
                $('<p/>', {text: algName + TempoUtils.locale("mine.regressionResultWell")}).appendTo(oResultContentDiv);
            } else {
                $('<p/>', {text: algName + TempoUtils.locale("mine.regressionResultBad")}).appendTo(oResultContentDiv);
            }
            $('<p/>', {text: TempoUtils.locale("mine.regressionRelativeError") + TempoMineUtils.toPercent(relativeError)}).appendTo(oResultContentDiv);
        }
    }
}

/**
 * 渲染聚类评估的结果描述信息
 */
window.rendClusterEvalResult = function (oResultContentDiv, clusterEvalData) {
    if (!clusterEvalData) {
        return;
    }
    for (var i = 0; i < clusterEvalData.length; i++) {
        var clusterEvalDataItem = clusterEvalData[i];
        var algName2ResultData = clusterEvalDataItem.algName2ResultData;
        for (var p in algName2ResultData) {
            var algName2ResultDataItem = algName2ResultData[p].split('|');
            if (algName2ResultDataItem.length == 2) {
                var profileCoef = algName2ResultDataItem[0];
                var classNum = JSON.parse(algName2ResultDataItem[1] || {});
                if (profileCoef > 0.8) {
                    $('<p/>', {text: p + TempoUtils.locale("mine.culsterResultPreferably").replace("profileCoef", profileCoef)}).appendTo(oResultContentDiv);
                } else if (profileCoef >= 0.5) {
                    $('<p/>', {text: p + TempoUtils.locale("mine.culsterResultWell").replace("profileCoef", profileCoef)}).appendTo(oResultContentDiv);
                } else if (profileCoef > 0) {
                    $('<p/>', {text: p + TempoUtils.locale("mine.culsterResultBad").replace("profileCoef", profileCoef)}).appendTo(oResultContentDiv);
                }

                for (var i in classNum) {
                    var clusterClassDesc = TempoUtils.locale('mine.clusterclassdesc').replace('num', parseInt(i) + 1 + "").replace('count', classNum[i]);
                    $('<p/>', {text: clusterClassDesc}).appendTo(oResultContentDiv);
                }
            }
        }
    }
}

/**
 * 渲染概览-流程图描述的结果描述信息
 */
window.rendResult = function (oResultContentDiv, resultData) {
    var clusterData = resultData.clusterData;
    var clusterEvalData = resultData.clusterEvalData;
    var classifiData = resultData.classifiData;
    var classifiEvalData = resultData.classifiEvalData;
    var regressionData = resultData.regressionData;
    var regressionEvalData = resultData.regressionEvalData;

    rendClusterResult(oResultContentDiv, clusterData);
    rendClusterEvalResult(oResultContentDiv, clusterEvalData);
    rendClassifiResult(oResultContentDiv, classifiData);
    rendClassifiEvalResult(oResultContentDiv, classifiEvalData);
    rendRegressionResult(oResultContentDiv, regressionData);
    rendRegressionEvalResult(oResultContentDiv, regressionEvalData);
}

/**
 * 创建洞察流程图描述：综述、输入、过程和结果的div布局
 */
window.createFlowDescLayout = function (descBoard) {
    $('<div/>', {
        class: "tempo-mine-input-flow-view-overview"
    }).append($('<p/>', {
        text: TempoUtils.locale("mine.summarize")
    })).appendTo(descBoard);

    var oOverViewContentDiv = $('<div/>', {
        class: "tempo-mine-input-flow-view-overview-content"
    }).appendTo(descBoard);

    $('<div/>', {
        class: "tempo-mine-input-flow-view-input"
    }).append($('<p/>', {
        text: TempoUtils.locale("mine.input")
    })).appendTo(descBoard);

    var oInputContentDiv = $('<div/>', {
        class: "tempo-mine-input-flow-view-input-content"
    }).appendTo(descBoard);

    $('<div/>', {
        class: "tempo-mine-input-flow-view-process"
    }).append($('<p/>', {
        text: TempoUtils.locale("mine.mine_process")
    })).appendTo(descBoard);

    var oProcessContentDiv = $('<div/>', {
        class: "tempo-mine-input-flow-view-process-content"
    }).appendTo(descBoard);

    $('<div/>', {
        class: "tempo-mine-input-flow-view-result"
    }).append($('<p/>', {
        text: TempoUtils.locale("mine.result")
    })).appendTo(descBoard);

    var oResultContentDiv = $('<div/>', {
        class: "tempo-mine-input-flow-view-result-content"
    }).appendTo(descBoard);

    var layoutDivObj = {
        oOverViewContentDiv: oOverViewContentDiv,
        oInputContentDiv: oInputContentDiv,
        oProcessContentDiv: oProcessContentDiv,
        oResultContentDiv: oResultContentDiv
    }
    return layoutDivObj;
}

window.resultData = {};

/**
 * 显示洞察->概览->流程图描述的内容
 */
window.showFlowDesc = function (insightInfoList, descBoard, pid, analyseSubjectName) {
    var overViewData = {};
    var inputData = {};
    var processData = [];
    var preprocessData = [];
    var clusterData = [];
    var clusterEvalData = [];
    var classifiData = [];
    var classifiEvalData = [];
    var regressionData = [];
    var regressionEvalData = [];
    resultData = {};
    overViewData.analyseSubjectName = analyseSubjectName;
    if (insightInfoList && insightInfoList.length > 0) {
        for (var i = 0; i < insightInfoList.length; i++) {
            var insightInfoItem = insightInfoList[i];
            var insightInfoJsonItem = JSON.parse(insightInfoItem.info || {});
            if (insightInfoItem) {
                if (insightInfoItem.nodeType == window.mineConstant.NODE_CATEGORY_PROCESS) {
                    overViewData.startTime = insightInfoJsonItem.startTime;
                } else if (insightInfoItem.nodeType == window.mineConstant.NODE_CATEGORY_END) {
                    overViewData.durationTime = insightInfoJsonItem.durationTime;
                    overViewData.endTime = insightInfoJsonItem.endTime;
                    overViewData.nodeNum = insightInfoJsonItem.nodeNum;
                    overViewData.nodeNames = insightInfoJsonItem.nodeNames;
                    var names = insightInfoJsonItem.nodeNames.split(',');
                    var arr = $.grep(names, function (n, m) {
                        return $.inArray(n, names, m + 1) == -1;
                    });
                    overViewData.nodeNum = arr.length;
                    overViewData.nodeNames = arr.join(',');
                } else if (insightInfoItem.nodeType == window.mineConstant.NODE_CATEGORY_INPUT &&
                    insightInfoItem.portType == window.mineConstant.PORT_NODE_DEFAULT) {
                    inputData.tableName = insightInfoJsonItem.tableName;
                    inputData.colNum = insightInfoJsonItem.colNum;
                    inputData.rowNum = insightInfoJsonItem.rowNum;
                    inputData.strNum = insightInfoJsonItem.strNum;
                    inputData.dateNum = insightInfoJsonItem.dateNum;
                    inputData.numricNum = insightInfoJsonItem.numricNum;
                    inputData.fullColNames = insightInfoJsonItem.fullColNames;
                    inputData.missColNames = insightInfoJsonItem.missColNames;
                } else if ((insightInfoJsonItem.type == window.mineConstant.NODE_TYPE_PREPROCESS &&
                    insightInfoItem.portType == window.mineConstant.PORT_NODE_DEFAULT) ||
                    (insightInfoJsonItem.type == "minechart" && insightInfoItem.portType == "graph")) {
                    processData.push(insightInfoItem);
                }

                if (insightInfoJsonItem.locType == window.mineConstant.RESULT) {
                    if (insightInfoJsonItem.type == window.mineConstant.NODE_TYPE_PREPROCESS &&
                        insightInfoItem.portType == window.mineConstant.PORT_NODE_DEFAULT) {
                        preprocessData.push(insightInfoItem);
                    } else if (insightInfoItem.nodeType == window.mineConstant.NODE_CATEGORY_CLUSTRING &&
                        insightInfoItem.portType == window.mineConstant.PORT_MODEL) {
                        var obj = {};
                        obj.nodeName = insightInfoItem.nodeLabel;
                        obj.data = insightInfoJsonItem.classNum;
                        clusterData.push(obj);
                    } else if (insightInfoItem.nodeType == window.mineConstant.NODE_CATEGORY_REGRESSION &&
                        insightInfoItem.portType == window.mineConstant.PORT_MODEL) {
                        var obj = {};
                    } else if (insightInfoItem.nodeType == window.mineConstant.NODE_CATEGORY_CLASSIFICATION &&
                        insightInfoItem.portType == window.mineConstant.PORT_MODEL) {
                        var obj = {};
                    } else if (insightInfoJsonItem.evalType == window.mineConstant.CLUSTER_EVAL &&
                        insightInfoItem.portType == window.mineConstant.PORT_MODEL) {
                        var obj = {};
                        obj.algName2ResultData = insightInfoJsonItem.algName2ResultData;
                        clusterEvalData.push(obj);
                    } else if (insightInfoJsonItem.evalType == window.mineConstant.CLASSIFICATION_EVAL &&
                        insightInfoItem.portType == window.mineConstant.PORT_MODEL) {
                        var obj = {};
                        obj.algName2CorrectRation = insightInfoJsonItem.algName2CorrectRation;
                        classifiEvalData.push(obj);
                    } else if (insightInfoJsonItem.evalType == window.mineConstant.REGRESSION_EVAL &&
                        insightInfoItem.portType == window.mineConstant.PORT_MODEL) {
                        var obj = {};
                        obj.algName2relativeError = insightInfoJsonItem.algName2relativeError;
                        regressionEvalData.push(obj);
                    }
                }
            }
        }
        resultData.overViewData = overViewData;
        resultData.clusterData = clusterData;
        resultData.clusterEvalData = clusterEvalData;
        resultData.classifiData = classifiData;
        resultData.classifiEvalData = classifiEvalData;
        resultData.regressionData = regressionData;
        resultData.regressionEvalData = regressionEvalData;
        resultData.inputData = inputData;
        resultData.processData = processData;
    }
}

/**
 * 输入
 * 当为新流程时未空，加载洞察导致页面报错
 */
window.renderSummaryInput = function (callback, inputData) {
    if (inputData && !$.isEmptyObject(inputData)) {
        var container = callback();
        rendDescInput(container, inputData);
    }
}

/**
 * 结果
 *  当为新流程时未空，加载洞察导致页面报错
 */
window.renderSummaryResult = function (container, resultData) {
    if (resultData && !$.isEmptyObject(resultData)) {
        rendResult(container, resultData);
    }
}